package com.zjut.zs.util.tools;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import javax.annotation.Resource;

import org.json.JSONArray;
import org.json.JSONObject;

import com.zjut.an.config.dao.AnConfig;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.util.system.Status;
import com.zjut.zs.applyview.dao.ZsApplyViewDAO;
import com.zjut.zs.school.dao.ZsSchool;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.userstatus.dao.ZsUserStatus;


public class ZsHTMLCreat {
	
	@Resource(name = "AnConfigManager")
	private AnConfigManager anconfig;
	
	private String receivenoqz;
	public ZsHTMLCreat() {
		
	}

	public String getJsonValue(String name) {
		String HTML = "";
		try {					
			String jsonstring = this.getJsonString("/config.json");			
			JSONObject jo=new JSONObject(jsonstring); //json-lib中是jo=JSONObject.fromObject(t);	
			HTML = jo.getString(name);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return HTML;
	}
	
	public StringBuffer getSelectHTML(String selectname, String selectvalue, String addparameter) {
		StringBuffer HTML = new StringBuffer();
		HTML.append("<select name='"+selectname+"' id='"+selectname+"' "+addparameter+">");
		try {					
			String jsonstring = this.getJsonString("/config.json");			
			JSONObject jo=new JSONObject(jsonstring); //json-lib中是jo=JSONObject.fromObject(t);
			JSONArray list=jo.getJSONArray(selectname); //得到数组
			
			for (int i = 0; i < list.length(); i++) { 
				JSONObject valuejson=(JSONObject) list.get(i);  
				String value =(String) valuejson.get("value");  
				
				HTML.append("<option value='"+value+"'");
				if (value.equals(selectvalue)) {
					HTML.append(" selected");
				}
				HTML.append(">"+value+"</option>");
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}	
		HTML.append("</select>");
		return HTML;
	}
	
	public StringBuffer getSelectHTML(String selectname, String selectvalue, String addparameter, List list) {
		StringBuffer HTML = new StringBuffer();
		HTML.append("<select name='"+selectname+"' id='"+selectname+"' "+addparameter+">");
		try {					
			
			for (int i = 0; i < list.size(); i++) { 
				AnConfig config = (AnConfig)list.get(i);
				String value = config.getCoValue();
				HTML.append("<option value='"+value+"'");
				if (value.equals(selectvalue)) {
					HTML.append(" selected");
				}
				HTML.append(">"+value+"</option>");
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}	
		HTML.append("</select>");
		return HTML;
	}
	
	protected String getJsonString(String urlPath) throws Exception { 
		InputStream inputStream =  this.getClass().getResourceAsStream("/config.json");
        //对应的字符编码转换  
        Reader reader = new InputStreamReader(inputStream, "UTF-8");  
        BufferedReader bufferedReader = new BufferedReader(reader);  
        String str = null;  
        StringBuffer sb = new StringBuffer();  
        while ((str = bufferedReader.readLine()) != null) {  
            sb.append(str);  
        }  
        reader.close();  
        return sb.toString();  
    }  
	
	public String getInputString(String value) {
		if (value==null || "null".equals(value.trim()) || "NULL".equals(value.trim())) {
			return "";
		} else {
			return value;
		}
	}
	
	public String getMenuString(ZsSchool obj, ZsUserStatus userstatus, String type, String href, String menuname) {
		if ("scRegActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScRegActive())) {
				if (Status.ON.getStatus().equals(obj.getScRegActive())) { 
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			} 
		}
		if ("scLoginActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScLoginActive())) {
				if (Status.ON.getStatus().equals(obj.getScLoginActive())) { 
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			} 
		}
		if ("scCjActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScCjActive())) {
				if (Status.ON.getStatus().equals(obj.getScCjActive())) { 
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scExvalueActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScExvalueActive())) {
				return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";			
			}
		}
		if ("scZxlbActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScZxlbActive())) {
				return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>"; 				
			}
		}
		if ("scShhdActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScShhdActive())) {
				return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";			
			}
		}
		if ("scHjqkActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScHjqkActive())) {
				return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";			
			}
		}
		if ("scGrcsActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScGrcsActive())) {
				if (Status.ON.getStatus().equals(obj.getScGrcsActive())) { 
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scAppActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScAppActive())) {
				if (Status.ON.getStatus().equals(obj.getScAppActive())) { 
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scBackActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScBackActive())) {
				if (Status.ON.getStatus().equals(obj.getScBackActive()) && Status.ON.getStatus().equals(obj.getScAppActive()) && Status.SENDSTATUS_YES.getStatus().equals(userstatus.getSendStatus())) {
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scPrintActive".equals(type)) {
			return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
			/*if (Status.ON.getStatus().equals(obj.getScAppActive())) {//信息表已经确认
				return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
			} else {
				return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
			}*/
		}
		if ("scFilesendActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScFilesendActive())) {
				//if (Status.APPSTATUS_NO.getStatus().equals(userstatus.getAppStatus())) {
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				//} else {
				//	return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				//}				
			}
		}
		if ("scNoticeActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScNoticeActive())) {//允许打印、初审通过，准考证号已分配显示测试通知单
				if (Status.ON.getStatus().equals(obj.getScNoticeActive()) && Status.CHECKSTATUS_YES.getStatus().equals(userstatus.getCheckStatus())) {
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scPrintExamActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScPrintActive())) {//允许打印、初审通过，准考证号已分配显示打印
				if (Status.ON.getStatus().equals(obj.getScPrintActive()) && Status.CHECKSTATUS_YES.getStatus().equals(userstatus.getCheckStatus()) && null!=userstatus.getExId()) {
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scPayActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScPayActive())) {//允许支付、初审通过显示支付页面
				if (Status.ON.getStatus().equals(obj.getScPayActive())) {
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scSearchActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScSearchActive())) {//允许支付、初审通过显示支付页面
				if (Status.ON.getStatus().equals(obj.getScSearchActive())) {
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		if ("scEmsActive".equals(type)) {
			if (!Status.HIDDEN.getStatus().equals(obj.getScEmsActive())) {//允许录入EMS邮件
				if (Status.ON.getStatus().equals(obj.getScEmsActive())) {
					return "<li><cite></cite><a href="+href+" target='rightFrame' >"+menuname+"</a><i></i></li>";
				} else {
					return "<li><cite></cite><a href=\"javascript:alert('尚未满足条件或者尚未启用该功能');\" style='color:#666;' title = '尚未满足条件或者尚未启用该功能'>"+menuname+"</a><i></i></li>";
				}				
			}
		}
		return ""; 
	}
	/*
	public String getHrefString(ZsSchool obj, ZsUserStatus userstatus, String type, String href) {
		
		return "";
	}
	*/
	/*
	 * public String getReceiveNo(int usNo) { if (null == this.receivenoqz) {
	 * receivenoqz = anconfig.getSingleConfigInfoByName("报名号前缀"); } String receiveno
	 * = receivenoqz; if (usNo>99999) { receiveno = receiveno + usNo; } else if
	 * (usNo>9999) { receiveno = receiveno + "0" + usNo; } else if (usNo>999) {
	 * receiveno = receiveno + "00" + usNo; } else if (usNo>99) { receiveno =
	 * receiveno + "000" + usNo; } else if (usNo>9) { receiveno = receiveno + "0000"
	 * + usNo; } else { receiveno = receiveno + "00000" + usNo; } return receiveno;
	 * }
	 */
	/**
	 * 测试类主程序，建立数据库连接并在student表中修改记录
	 */
	public static void main(String[] args) {
		try {
			ZsHTMLCreat creat = new ZsHTMLCreat();
			StringBuffer str = creat.getSelectHTML("sex","女",null);

			System.out.println(str);
			System.out.println("read the properties file successful!");
		} catch (Exception e) {
			System.out.println("read the properties file error!");
			e.printStackTrace();
		}
	}
}
